'use strict'

import { app, BrowserWindow, Tray, ipcMain } from 'electron'
const path = require('path')
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

// var customResizable = false
var w = 443
var h = 501
function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    webPreferences: {webSecurity: false},
    width: w,
    height: h,
    useContentSize: true
    // resizable: customResizable
  })

  mainWindow.loadURL(winURL)
  showTray()
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

let tray = null
function showTray () {
  const iconName = 'icon.ico'
  const iconPath = path.join(__static, iconName)
  tray = new Tray(iconPath)
  tray.setToolTip('云桌面')
}
app.on('ready', createWindow)

app.on('blur', createWindow)

app.on('window-all-closed', () => {
  if (tray) {
    tray.destroy()
  }
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
// 处理子进程传来的数据
ipcMain.on('login_change', e => {
  mainWindow.maximize()
})
